package com.example.zhangpeng.spannabletest;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView text= (TextView) findViewById(R.id.tv_content);
        SpannableString spannableString=new SpannableString(getResources().getString(R.string.text));

        spannableString.setSpan(new URLSpan("http://www.baidu.com"),17,31, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new IntentSpan(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
            }
        }),32,35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        int color=getResources().getColor(R.color.blue);
        ForegroundColorSpan foregroundColorSpan=new ForegroundColorSpan(color);
        spannableString.setSpan(foregroundColorSpan,17,31, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(foregroundColorSpan,32,35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        assert text != null;
        text.setMovementMethod(LinkMovementMethod.getInstance());
        text.setText(spannableString);

    }
}
